$(function () {
    validateRule();

    $('.imgcode').click(function() {
        var url = ctx + "captcha/captchaImage?type=" + captchaType + "&s=" + Math.random();
        $(".imgcode").attr("src", url);
    });

    //使用手机号+验证码登录
    $("#loginByMobile").click(function () {
        loginType = "mobile";
        $("#loginByMobile").hide();
        $("#loginByUsername").show();
        $("#loginByUsernameForm").hide();
        $("#loginByMobileForm").show();
    });

    //使用账号+密码登录
    $("#loginByUsername").click(function () {
        loginType = "username";
        $("#loginByUsername").hide();
        $("#loginByMobile").show();
        $("#loginByUsernameForm").show();
        $("#loginByMobileForm").hide();
    });

    //获取验证码
    $("#btnGetVcode").click(function () {
        if (waitTime > 0) {
            return;
        }
        var mobile = $.common.trim($("input[name='mobile']").val());
        if (!validMobile(mobile)) {
            $.modal.msg("请输入正确的手机号码");
            return false;
        }

        $.modal.loading("正在发送短信验证码...");
        $.ajax({
            type: "POST",
            url: ctx + "sendVcode",
            data: {"mobile": mobile,"type": "login"},
            success: function (r) {
                $.modal.closeLoading();
                if (r.code === 0) {
                    $.modal.msgSuccess("短信发送成功");
                    //验证码发送成功
                    $("#btnGetVcode").removeClass("btn-info");
                    $("#btnGetVcode").addClass("btn-default");
                    //验证码跑秒递减处理
                    waitTime = 60; //默认等待60秒
                    var timeObj = setInterval(function () {
                        var validateCode = $("#vcode").val();//校验码input的对象
                        if (waitTime === 0) {
                            clearInterval(timeObj);
                            activeBtnGetVcode();
                        } else if (validateCode !== "" && validateCode.length >= 6) {
                            clearInterval(timeObj);
                            activeBtnGetVcode();
                        } else {
                            $("#btnGetVcode").text("请等待" + (waitTime--) + "秒");
                        }
                    }, 1000);
                } else {
                    //验证码发送失败
                    $.modal.msgError(r.msg);
                }
            }
        });
    });


    //重置密码，获取的验证码
    $("#btnGetFindPasswordVcode").click(function () {
        if (waitTime > 0) {
            return;
        }
        var mobile = $.common.trim($("input[name='resetPasswordMobile']").val());
        if (!validMobile(mobile)) {
            $.modal.msg("请输入正确的手机号码");
            return false;
        }

        $.modal.loading("正在发送短信验证码...");
        $.ajax({
            type: "POST",
            url: ctx + "sendVcode",
            data: {"mobile": mobile,"type": "resetpwd"},
            success: function (r) {
                $.modal.closeLoading();
                if (r.code === 0) {
                    $.modal.msgSuccess("短信发送成功");
                    //验证码发送成功
                    $("#btnGetFindPasswordVcode").removeClass("btn-info");
                    $("#btnGetFindPasswordVcode").addClass("btn-default");
                    //验证码跑秒递减处理
                    waitTime = 60; //默认等待60秒
                    var timeObj = setInterval(function () {
                        var validateCode = $("#findVcode").val();//校验码input的对象
                        if (waitTime === 0) {
                            clearInterval(timeObj);
                            activeBtnGetFindPasswordVcode();
                        } else if (validateCode !== "" && validateCode.length >= 6) {
                            clearInterval(timeObj);
                            activeBtnGetFindPasswordVcode();
                        } else {
                            $("#btnGetFindPasswordVcode").text("请等待" + (waitTime--) + "秒");
                        }
                    }, 1000);
                } else {
                    //验证码发送失败
                    $.modal.msgError(r.msg);
                }
            }
        });
    });

    //登录按钮
    $("#btnSubmit").click(function () {
        if (loginType === "username") {
            $("#loginByUsernameForm").submit();
        } else {
            $("#loginByMobileForm").submit();
        }
    });

    //重置密码
    $("#findPassword").click(function () {
        $(".clear-input").val("");
        $("#loginbox").hide();
        $("#findBox").show();
        $("#saveNewPasswordForm").hide();
        $("#resetPasswordByMobileForm").show();
    });

    //找回密码，验证 验证码是否有效
    $("#btnFindPassword").click(function () {
        checkPhoneCode();
    });
    //提交新密码
    $("#btnSubmitPassword").click(function () {
        saveNewPassword();
    });

    $(".find-loginByUsername").click(function(){
        $("#findBox").hide();
        $("#loginbox").show();
        $("#loginByUsernameForm").show();
        $("#loginByMobileForm").hide();
    });

});

$.validator.setDefaults({
    submitHandler: function () {
        login();
    }
});

//激活获取验证码按钮
function activeBtnGetVcode() {
    $("#btnGetVcode").addClass("btn-info");
    $("#btnGetVcode").removeClass("btn-default");
    $("#btnGetVcode").text("获取验证码");
}

//激活获取验证码按钮(找回密码)
function activeBtnGetFindPasswordVcode() {
    $("#btnGetFindPasswordVcode").addClass("btn-info");
    $("#btnGetFindPasswordVcode").removeClass("btn-default");
    $("#btnGetFindPasswordVcode").text("获取验证码");
}

//校验手机号码
function validMobile(value) {
    var length = value.length;
    return (length === 11 && /^((1[0-9]{2})+\d{8})$/.test(value));
}

function login() {
    $.modal.loading("登录中，请稍候...");
    var username = $.common.trim($("input[name='username']").val());
    var password = $.common.trim($("input[name='password']").val());
    var mobile = $.common.trim($("input[name='mobile']").val());
    var vcode = $.common.trim($("input[name='vcode']").val());
    var validateCode = $("input[name='validateCode']").val();
    var rememberMe = $("input[name='rememberme']").is(':checked');
    $.ajax({
        type: "post",
        url: ctx + "login",
        data: {
            "username": username,
            "password": password,
            "loginType": loginType,
            "mobile": mobile,
            "vcode": vcode,
            "validateCode" : validateCode,
            "rememberMe": rememberMe
        },
        success: function (r) {
            if (r.code === 0) {
                location.href = ctx + 'index';
            } else {
                $.modal.closeLoading();
                $('.imgcode').click();
                $.modal.msgError(r.msg);
            }
        }
    });
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#loginByUsernameForm").validate({
        rules: {
            username: {
                required: true
            },
            password: {
                required: true
            },
            validateCode: {
                required: true
            }
        },
        messages: {
            username: {
                required: icon + "请输入用户名"
            },
            password: {
                required: icon + "请输入密码"
            },
            validateCode: {
                required: icon + "请输入验证码"
            }
        }
    });
    $("#loginByMobileForm").validate({
        rules: {
            mobile: {
                required: true
            },
            vcode: {
                required: true
            }
        },
        messages: {
            mobile: {
                required: icon + "请输入手机号码"
            },
            vcode: {
                required: icon + "请输入短信验证码"
            }
        }
    });
    // $("#resetPasswordByMobileForm").validate({
    //     rules: {
    //         resetPasswordMobile: {
    //             required: true
    //         },
    //         findVcode: {
    //             required: true
    //         }
    //     },
    //     messages: {
    //         resetPasswordMobile: {
    //             required: icon + "请输入手机号码2"
    //         },
    //         findVcode: {
    //             required: icon + "请输入短信验证码"
    //         }
    //     }
    // })
}

//验证短信验证码是否有效
function checkPhoneCode() {
    var mobile = $.common.trim($("input[name='resetPasswordMobile']").val());
    var vcode = $.common.trim($("input[name='findVcode']").val());
    // if(mobile=="" || vcode==""){
    //     return false;
    // }
    if (!validMobile(mobile)) {
        $.modal.msg("手机号码不正确！");
        return false;
    }
    if (vcode=="") {
        $.modal.msg("验证码不能为空");
        return false;
    }
    $.ajax({
        type: "POST",
        url: ctx + "checkVcode",
        data: {
            "mobile": mobile,
            "vcode": vcode
        },
        success: function (r) {
            if (r.code === 0) {
                //$(".clear-input").val("");
                $("#resetPasswordByMobileForm").hide();
                $("#saveNewPasswordForm").show();
                $("input[name='findUsername']").val(r.loginName);
            } else {
                // $.modal.closeLoading();
                // $('.imgcode').click();
                $.modal.msgError(r.msg);

            }
        }
    });
}


//保存新密码
function saveNewPassword() {
    var newPassword = $.common.trim($("input[name='newPassword']").val());
    var newPassword2 = $.common.trim($("input[name='newPassword2']").val());
    var loginName = $.common.trim($("input[name='findUsername']").val());
    var mobile = $.common.trim($("input[name='resetPasswordMobile']").val());
    // if(mobile=="" || vcode==""){
    //     return false;
    // }
    if(loginName==""){
        $.modal.msg("用户名不存在！！");
        return false;
    }
    if (newPassword=="" || newPassword2=="") {
        $.modal.msg("新密码不能为空！！");
        return false;
    }else{
        if(newPassword!=newPassword2){
            $.modal.msg("两次新密码不一致！！");
            return false;
        }
    }
    $.ajax({
        type: "POST",
        url: ctx + "saveNewPassword",
        data: {
            "newPassword": newPassword,
            "loginName":loginName,
            "mobile":mobile
        },
        success: function (r) {
            if (r.code === 0) {
                $.modal.msgSuccess("密码重置成功！");
                $("#findBox").hide();
                $("#loginbox").show();

            } else {
                $.modal.msgError(r.msg);

            }
        }
    });
}